package com.hicorp.segment.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BasicSqlOption {

    /**
     * 查询所有
     *
     * @param tableName 被查的主表名称
     * @param log       日志
     * @return 查询结果
     */
    public static Object selectAll(String tableName, Logger log) {
        try {
            Class<?> clazz = Class.forName("com.hicorp.segment.mapper." + tableName + "Mapper");
            char[] chars = tableName.toCharArray();
            chars[0] = Character.toLowerCase(chars[0]);
            String MapperName = String.valueOf(chars) + "Mapper";
            Object mapper = SpringUtil.getBean(MapperName);
            Method method = clazz.getMethod("selectAll");
            return method.invoke(mapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public static Object changeRecord(Object record, String tableName, String upperCamelTableName, Class<?> tableClass, String operator) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        // 小驼峰mapper名, 用于注入.
        String lowerCamelTableName = BasicSqlOption.setCamel2LowerCamel(tableName);

        // mapper的Class
        Class<?> mapperClass = Class.forName("com.hicorp.segment.mapper." + upperCamelTableName + "Mapper");

        // Mapper名(小驼峰名，用于注入)
        String mapperName = lowerCamelTableName + "Mapper";

        // 利用SpringUtil工具类注入Mapper.
        Object masterMapper = SpringUtil.getBean(mapperName);

        // 利用反射获取方法.
        Method method = mapperClass.getMethod(operator, Object.class);

        // 将record转换成具体的pojo类型.
        ObjectMapper objectMapper = new ObjectMapper();
        Object convertValue = objectMapper.convertValue(record, tableClass);

        // 利用反射执行新增或编辑方法.
        method.invoke(masterMapper, convertValue);
        return convertValue;
    }

    /**
     * 驼峰转大驼峰
     *
     * @param s 驼峰字符串
     * @return 大驼峰
     */
    public static String setCamel2UpperCamel(String s) {

        char[] upperCamelName = s.toCharArray();
        upperCamelName[0] = Character.toUpperCase(upperCamelName[0]);
        return String.valueOf(upperCamelName);
    }

    /**
     * 驼峰转小驼峰
     *
     * @param s 驼峰字符串
     * @return 小驼峰
     */
    public static String setCamel2LowerCamel(String s) {
        char[] lowerCamelName = s.toCharArray();
        lowerCamelName[0] = Character.toLowerCase(lowerCamelName[0]);
        return String.valueOf(lowerCamelName);
    }

    /**
     * 去掉- 转大驼峰
     * wark-table => WorkTable
     * @param s 驼峰字符串
     * @return 小驼峰
     */
    public static String TranFormName(String s) {
        String[] nameList=s.split("-");
        StringBuilder newName = new StringBuilder();
        for(String name : nameList){
            newName.append(setCamel2UpperCamel(name));
        }
        return newName.toString();
    }
}
